using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;

namespace WindowsUserControl.Database
{
    /// <summary>
    /// 
    /// </summary>
    public partial class DataSources : UserControl
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="connStr"></param>
        /// <param name="tblName"></param>
        public delegate void ViewTableStatistics(string connStr, string tblName);

        /// <summary>
        /// 
        /// </summary>
        public event ViewTableStatistics ViewTableStatisticsEvent;

        #region init
        /// <summary>
        /// 
        /// </summary>
        public DataSources()
        {
            InitializeComponent();
        }

        private void DataSources_Load(object sender, EventArgs e)
        {
            this.InitDatabaseSources();
        }

        private void InitDatabaseSources()
        {
            this.tree_Source.Nodes.Clear();
            Hashtable htHost = new Hashtable();
            string[] connStrs = StringUtil.Split(System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionStore"));
            if (connStrs != null && connStrs.Length > 0)
            {
                for (int i = 0; i < connStrs.Length; i++)
                {
                    string host = DbConnUtil.GetHost(connStrs[i]);
                    if (!htHost.Contains(host.ToLower()))
                    {
                        TreeNode serverTN = new TreeNode(host);
                        serverTN.SelectedImageIndex = 0;
                        serverTN.ImageIndex = 0;

                        DbConnection connObj = new DbConnection();
                        connObj.AuthType = DbConnUtil.GetAuthenticationType(connStrs[i]);
                        connObj.CatalogName = DbConnUtil.GetDatabaseName(connStrs[i]);
                        connObj.DataSource = DbConnUtil.GetHost(connStrs[i]);
                        string instance = DbConnUtil.GetDbServerInstance(connStrs[i]);
                        if (!string.IsNullOrEmpty(instance))
                        {
                            connObj.DataSource += "\\" + instance;
                        }
                        connObj.Password = DbConnUtil.GetPassword(connStrs[i]);
                        connObj.Provider = DataProviderType.MSSQL;
                        connObj.UserID = DbConnUtil.GetUserID(connStrs[i]);

                        serverTN.Tag = connObj;
                        this.tree_Source.Nodes.Add(serverTN);
                        htHost.Add(host.ToLower(), connStrs[i]);
                    }
                }
            }
        }
        #endregion

        #region context menu items
        private void tree_Source_MouseUp(object sender, MouseEventArgs e)
        {
            this.cmu_DataSource.Items.Clear();
            if (this.tree_Source.GetNodeAt(e.X, e.Y) != null)
            {
                this.tree_Source.SelectedNode = this.tree_Source.GetNodeAt(e.X, e.Y);
                
                ToolStripMenuItem mnuRefresh = new ToolStripMenuItem("Refresh");
                mnuRefresh.Click += new EventHandler(mnuRefresh_Click);
                this.cmu_DataSource.Items.Add(mnuRefresh);

                switch (this.tree_Source.SelectedNode.ImageIndex)
                {
                    case 0: // server
                        ToolStripMenuItem mnuConnect = new ToolStripMenuItem("Connect...");
                        mnuConnect.Click += new EventHandler(mnuConnect_Click);
                        this.cmu_DataSource.Items.Add(mnuConnect);
                        ToolStripMenuItem mnuDisconnect = new ToolStripMenuItem("Disconnect");
                        mnuDisconnect.Click += new EventHandler(mnuDisconnect_Click);
                        this.cmu_DataSource.Items.Add(mnuDisconnect);
                        ToolStripMenuItem mnuQuery = new ToolStripMenuItem("New query");
                        mnuQuery.Click += new EventHandler(mnuQuery_Click);
                        this.cmu_DataSource.Items.Add(mnuQuery);
                        break;
                    case 1: // db
                        ToolStripMenuItem mnuNewDB = new ToolStripMenuItem("New database");
                        mnuNewDB.Click += new EventHandler(mnuNewDB_Click);
                        this.cmu_DataSource.Items.Add(mnuNewDB);
                        ToolStripMenuItem mnuDropDB = new ToolStripMenuItem("Delete");
                        mnuDropDB.Click += new EventHandler(mnuDropDB_Click);
                        this.cmu_DataSource.Items.Add(mnuDropDB);
                        ToolStripMenuItem mnuQuery2 = new ToolStripMenuItem("New query");
                        mnuQuery2.Click += new EventHandler(mnuQuery2_Click);
                        this.cmu_DataSource.Items.Add(mnuQuery2);
                        break;
                    case 2: // folder
                        
                        break;
                    case 3: // table
                        ToolStripMenuItem mnuOpenTable = new ToolStripMenuItem("Open table");
                        mnuOpenTable.Click += new EventHandler(mnuOpenTable_Click);
                        this.cmu_DataSource.Items.Add(mnuOpenTable);
                        ToolStripMenuItem mnuRenameTable = new ToolStripMenuItem("Rename");
                        mnuRenameTable.Click += new EventHandler(mnuRenameTable_Click);
                        this.cmu_DataSource.Items.Add(mnuRenameTable);
                        ToolStripMenuItem mnuDropTable = new ToolStripMenuItem("Delete");
                        mnuDropTable.Click += new EventHandler(mnuDropTable_Click);
                        this.cmu_DataSource.Items.Add(mnuDropTable);
                        ToolStripMenuItem mnuTableStats = new ToolStripMenuItem("Statistics");
                        mnuTableStats.Click += new EventHandler(mnuTableStats_Click);
                        this.cmu_DataSource.Items.Add(mnuTableStats);
                        break;
                    case 4: // view
                        ToolStripMenuItem mnuOpenView = new ToolStripMenuItem("Open view");
                        mnuOpenView.Click += new EventHandler(mnuOpenView_Click);
                        this.cmu_DataSource.Items.Add(mnuOpenView);
                        ToolStripMenuItem mnuRenameView = new ToolStripMenuItem("Rename");
                        mnuRenameView.Click += new EventHandler(mnuRenameView_Click);
                        this.cmu_DataSource.Items.Add(mnuRenameView);
                        ToolStripMenuItem mnuDropView = new ToolStripMenuItem("Delete");
                        mnuDropView.Click += new EventHandler(mnuDropView_Click);
                        this.cmu_DataSource.Items.Add(mnuDropView);
                        break;
                    default:
                        break;
                }
            }
        }

        #endregion

        #region servers
        void mnuQuery_Click(object sender, EventArgs e)
        {
            if (this.tree_Source.SelectedNode != null && this.tree_Source.SelectedNode.ImageIndex == 0)
            {
                DbConnection dbConnObj = (DbConnection)this.tree_Source.SelectedNode.Tag;
                string connStr = DbConnUtil.BuildConnectionString(dbConnObj.DataSource, string.Empty, dbConnObj.CatalogName, dbConnObj.AuthType, dbConnObj.UserID, dbConnObj.Password);
                
                this.OpenNewQueryWindow(connStr);
            }
        }

        void mnuDisconnect_Click(object sender, EventArgs e)
        {
            if (this.tree_Source.SelectedNode != null && this.tree_Source.SelectedNode.ImageIndex == 0)
            {
                TreeNode serverTN = this.tree_Source.SelectedNode;
                this.tree_Source.Nodes.Remove(serverTN);
            }
        }

        void mnuConnect_Click(object sender, EventArgs e)
        {
            //Dialogs.NewDbConnectionDialog dlg = new Dialogs.NewDbConnectionDialog();
            //dlg.NewDatabaseConnectionEvent += new Dialogs.NewDatabaseConnection(dlg_NewDatabaseConnectionEvent);
            //dlg.Show(this);
            DatabaseBrowser dbBrowser = new DatabaseBrowser();
            dbBrowser.Show(this);
            dbBrowser.DatabaseSelectedEvent +=new DatabaseBrowser.DatabaseSelected(this.dlg_NewDatabaseConnectionEvent);
        }

        void dlg_NewDatabaseConnectionEvent(string connStr)
        {
            string host = DbConnUtil.GetHost(connStr);
            TreeNode serverTN = new TreeNode(host);
            serverTN.SelectedImageIndex = 0;
            serverTN.ImageIndex = 0;

            DbConnection connObj = new DbConnection();
            connObj.AuthType = DbConnUtil.GetAuthenticationType(connStr);
            connObj.CatalogName = DbConnUtil.GetDatabaseName(connStr);
            connObj.DataSource = DbConnUtil.GetHost(connStr);
            string instance = DbConnUtil.GetDbServerInstance(connStr);
            if (!string.IsNullOrEmpty(instance))
            {
                connObj.DataSource += "\\" + instance;
            }
            connObj.Password = DbConnUtil.GetPassword(connStr);
            connObj.Provider = DataProviderType.MSSQL;
            connObj.UserID = DbConnUtil.GetUserID(connStr);

            serverTN.Tag = connObj;
            this.tree_Source.Nodes.Add(serverTN);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serverTN"></param>
        private void RefreshServer(TreeNode serverTN)
        {
            if (this.tree_Source.SelectedNode != null && this.tree_Source.SelectedNode.ImageIndex == 0)
            {
                DbConnection dbConnObj = (DbConnection)this.tree_Source.SelectedNode.Tag;
                string connStr = DbConnUtil.BuildConnectionString(dbConnObj.DataSource, string.Empty, dbConnObj.CatalogName, dbConnObj.AuthType, dbConnObj.UserID, dbConnObj.Password);
                
                string[] dbNames = DbUtil.GetDatabases(connStr);
                Hashtable htDbs = new Hashtable();
                foreach (string dbName in dbNames)
                {
                    DbConnection connObj = new DbConnection();
                    connObj.AuthType = DbConnUtil.GetAuthenticationType(connStr);
                    connObj.CatalogName = dbName;
                    connObj.DataSource = DbConnUtil.GetHost(connStr);
                    string instance = DbConnUtil.GetDbServerInstance(connStr);
                    if (!string.IsNullOrEmpty(instance))
                    {
                        connObj.DataSource += "\\" + instance;
                    }
                    connObj.Password = DbConnUtil.GetPassword(connStr);
                    connObj.Provider = DataProviderType.MSSQL;
                    connObj.UserID = DbConnUtil.GetUserID(connStr);
                    htDbs.Add(dbName.ToLower(), connObj);
                }

                if (this.tree_Source.SelectedNode.Nodes.Count > 0)
                {
                    foreach (TreeNode dbTN in this.tree_Source.SelectedNode.Nodes)
                    {
                        if (htDbs.Contains(dbTN.Text.ToLower()))
                        {
                            htDbs.Remove(dbTN.Text.ToLower());
                        }
                    }
                }

                if (htDbs.Count > 0)
                {
                    for (int i = 0; i < dbNames.Length; i++)
                    {
                        if (htDbs.Contains(dbNames[i].ToLower()))
                        {
                            TreeNode dbTn = new TreeNode(dbNames[i]);
                            dbTn.ImageIndex = 1;
                            dbTn.SelectedImageIndex = 1;
                            dbTn.Tag = htDbs[dbNames[i].ToLower()];
                            this.tree_Source.SelectedNode.Nodes.Add(dbTn);
                        }
                    }
                }
            }
        }
        #endregion

        #region databases
        void mnuQuery2_Click(object sender, EventArgs e)
        {
            if (this.tree_Source.SelectedNode != null && this.tree_Source.SelectedNode.ImageIndex == 1)
            {
                DbConnection dbConnObj = (DbConnection)this.tree_Source.SelectedNode.Tag;
                string connStr = DbConnUtil.BuildConnectionString(dbConnObj.DataSource, string.Empty, dbConnObj.CatalogName, dbConnObj.AuthType, dbConnObj.UserID, dbConnObj.Password);
                this.OpenNewQueryWindow(connStr);
            }
        }

        void mnuDropDB_Click(object sender, EventArgs e)
        {
            if (this.tree_Source.SelectedNode != null && this.tree_Source.SelectedNode.ImageIndex == 1)
            {
                string dbName = this.tree_Source.SelectedNode.Text;
                DialogResult result = MessageBox.Show("Are you sure to drop database [" + dbName + "]?", "Drop database", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    DbConnection dbConnObj = (DbConnection)this.tree_Source.SelectedNode.Tag;
                    string connStr = DbConnUtil.BuildConnectionString(dbConnObj.DataSource, string.Empty, dbConnObj.CatalogName, dbConnObj.AuthType, dbConnObj.UserID, dbConnObj.Password);
                    bool dropped = DbUtil.DropDatabase(connStr, dbName);
                    if (dropped)
                    {
                        this.tree_Source.SelectedNode.Parent.Nodes.Remove(this.tree_Source.SelectedNode);
                    }
                }
            }
        }

        void mnuNewDB_Click(object sender, EventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        private void RefreshDatabase(TreeNode dbTN)
        {
            if (this.tree_Source.SelectedNode != null && this.tree_Source.SelectedNode.ImageIndex == 1)
            {
                DbConnection dbConnObj = (DbConnection)this.tree_Source.SelectedNode.Tag;
                string connStr = DbConnUtil.BuildConnectionString(dbConnObj.DataSource, string.Empty, dbConnObj.CatalogName, dbConnObj.AuthType, dbConnObj.UserID, dbConnObj.Password);
                string[] tableNames = DbUtil.GetTables(connStr);
                Hashtable htTables = new Hashtable();
                if (tableNames != null && tableNames.Length > 0)
                {
                    foreach (string tblName in tableNames)
                    {
                        htTables.Add(tblName.ToLower(), dbConnObj);
                    }
                }
                TreeNode tableRootTN = null;
                string[] viewNames = DbUtil.GetViews(connStr);
                Hashtable htViews = new Hashtable();
                if (viewNames != null && viewNames.Length > 0)
                {
                    foreach (string viewName in viewNames)
                    {
                        htViews.Add(viewName.ToLower(), dbConnObj);
                    }
                }
                TreeNode viewRootTN = null;
                if (this.tree_Source.SelectedNode.Nodes.Count > 0)
                {
                    foreach (TreeNode schemaRootTN in this.tree_Source.SelectedNode.Nodes)
                    {
                        if (schemaRootTN.Text.ToLower() == "tables")
                        {
                            tableRootTN = schemaRootTN;
                            break;
                        }
                    }
                }
                if (this.tree_Source.SelectedNode.Nodes.Count > 0)
                {
                    foreach (TreeNode schemaRootTN in this.tree_Source.SelectedNode.Nodes)
                    {
                        if (schemaRootTN.Text.ToLower() == "views")
                        {
                            viewRootTN = schemaRootTN;
                            break;
                        }
                    }
                }

                if (tableRootTN != null)
                {
                    if (tableRootTN.Nodes.Count > 0)
                    {
                        foreach (TreeNode tableTN in tableRootTN.Nodes)
                        {
                            if (htTables.Contains(tableTN.Text.ToLower()))
                            {
                                htTables.Remove(tableTN.Text.ToLower());
                            }
                        }
                    }
                }
                else
                {
                    tableRootTN = new TreeNode("Tables");
                    tableRootTN.ImageIndex = 2;
                    tableRootTN.SelectedImageIndex = 2;
                    tableRootTN.Tag = dbConnObj;
                    this.tree_Source.SelectedNode.Nodes.Add(tableRootTN);
                }
                if (htTables.Count > 0)
                {
                    for (int i = 0; i < tableNames.Length; i++)
                    {
                        if (htTables.Contains(tableNames[i].ToLower()))
                        {
                            TreeNode tableTn = new TreeNode(tableNames[i]);
                            tableTn.ImageIndex = 3;
                            tableTn.SelectedImageIndex = 3;
                            tableTn.Tag = htTables[tableNames[i].ToLower()];
                            tableRootTN.Nodes.Add(tableTn);
                        }
                    }
                }


                if (viewRootTN != null)
                {
                    if (viewRootTN.Nodes.Count > 0)
                    {
                        foreach (TreeNode viewTn in viewRootTN.Nodes)
                        {
                            if (htViews.Contains(viewTn.Text.ToLower()))
                            {
                                htViews.Remove(viewTn.Text.ToLower());
                            }
                        }
                    }
                }
                else
                {
                    viewRootTN = new TreeNode("Views");
                    viewRootTN.ImageIndex = 2;
                    viewRootTN.SelectedImageIndex = 2;
                    viewRootTN.Tag = dbConnObj;
                    this.tree_Source.SelectedNode.Nodes.Add(viewRootTN);
                }
                if (htViews.Count > 0)
                {
                    for (int i = 0; i < viewNames.Length; i++)
                    {
                        if (htViews.Contains(viewNames[i].ToLower()))
                        {
                            TreeNode viewTn = new TreeNode(viewNames[i]);
                            viewTn.ImageIndex = 4;
                            viewTn.SelectedImageIndex = 4;
                            viewTn.Tag = htViews[viewNames[i].ToLower()];
                            viewRootTN.Nodes.Add(viewTn);
                        }
                    }
                }
            }
        }
        #endregion

        #region tables
        void mnuDropTable_Click(object sender, EventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        void mnuRenameTable_Click(object sender, EventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        void mnuOpenTable_Click(object sender, EventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableRootTN"></param>
        private void RefreshTables(TreeNode tableRootTN)
        {
            if (this.tree_Source.SelectedNode != null && this.tree_Source.SelectedNode.ImageIndex == 2)
            {
                DbConnection dbConnObj = (DbConnection)this.tree_Source.SelectedNode.Tag;
                string connStr = DbConnUtil.BuildConnectionString(dbConnObj.DataSource, string.Empty, dbConnObj.CatalogName, dbConnObj.AuthType, dbConnObj.UserID, dbConnObj.Password);
                string[] tableNames = DbUtil.GetTables(connStr);
                Hashtable htTables = new Hashtable();
                if (tableNames != null && tableNames.Length > 0)
                {
                    foreach (string tblName in tableNames)
                    {
                        htTables.Add(tblName.ToLower(), dbConnObj);
                    }
                }
                if (this.tree_Source.SelectedNode.Nodes.Count > 0)
                {
                    foreach (TreeNode tableTN in this.tree_Source.SelectedNode.Nodes)
                    {
                        if (htTables.Contains(tableTN.Text.ToLower()))
                        {
                            htTables.Remove(tableTN.Text.ToLower());
                        }
                    }
                }

                if (htTables.Count > 0)
                {
                    for (int i = 0; i < tableNames.Length; i++)
                    {
                        if (htTables.Contains(tableNames[i].ToLower()))
                        {
                            TreeNode tableTn = new TreeNode(tableNames[i]);
                            tableTn.ImageIndex = 3;
                            tableTn.SelectedImageIndex = 3;
                            tableTn.Tag = htTables[tableNames[i].ToLower()];
                            this.tree_Source.SelectedNode.Nodes.Add(tableTn);
                        }
                    }
                }
            }
        }

        private void RefreshTable(TreeNode tableTN)
        {
            if (this.tree_Source.SelectedNode != null && this.tree_Source.SelectedNode.ImageIndex == 3)
            {
                TreeNode tblTn = this.tree_Source.SelectedNode;
                DbConnection dbConnObj = (DbConnection)tblTn.Tag;
                string connStr = DbConnUtil.BuildConnectionString(dbConnObj.DataSource, string.Empty, dbConnObj.CatalogName, dbConnObj.AuthType, dbConnObj.UserID, dbConnObj.Password);
                string tblName = tblTn.Text;
                TableColumn[] cols = DbUtil.GetTableColumns(DataProviderType.MSSQL, connStr, string.Empty, tblName);
                tblTn.Nodes.Clear();
                foreach (TableColumn col in cols)
                {
                    string nodeText = col.ColumnName + " (" + col.DbType.ToString() + ")";
                    TreeNode colTn = new TreeNode(nodeText);
                    colTn.Tag = col.ColumnName;
                    colTn.ImageIndex = 7;
                    colTn.SelectedImageIndex = 7;
                    if (col.IsPK)
                    {
                        colTn.ImageIndex = 19;
                        colTn.SelectedImageIndex = 19;
                    }
                    else
                    {
                        switch (col.DbType)
                        {
                            case System.Data.SqlDbType.BigInt:
                            case System.Data.SqlDbType.Decimal:
                            case System.Data.SqlDbType.Float:
                            case System.Data.SqlDbType.Int:
                            case System.Data.SqlDbType.Money:
                            case System.Data.SqlDbType.Real:
                            case System.Data.SqlDbType.SmallInt:
                            case System.Data.SqlDbType.SmallMoney:
                            case System.Data.SqlDbType.TinyInt:
                                colTn.ImageIndex = 8;
                                colTn.SelectedImageIndex = 8;
                                break;
                            case SqlDbType.Char:
                            case SqlDbType.NChar:
                            case SqlDbType.NText:
                            case SqlDbType.NVarChar:
                            case SqlDbType.Text:
                            case SqlDbType.VarChar:
                                colTn.ImageIndex = 7;
                                colTn.SelectedImageIndex = 7;
                                break;
                            case SqlDbType.Bit:
                                colTn.ImageIndex = 9;
                                colTn.SelectedImageIndex = 9;
                                break;
                            case SqlDbType.DateTime:
                            case SqlDbType.SmallDateTime:
                                colTn.ImageIndex = 10;
                                colTn.SelectedImageIndex = 10;
                                break;
                            default:
                                break;
                        }
                    }
                    tblTn.Nodes.Add(colTn);
                }
            }
        }

        void mnuTableStats_Click(object sender, EventArgs e)
        {
            if (this.tree_Source.SelectedNode != null && this.tree_Source.SelectedNode.ImageIndex == 3)
            {
                TreeNode tblTn = this.tree_Source.SelectedNode;
                DbConnection dbConnObj = (DbConnection)tblTn.Tag;
                string connStr = DbConnUtil.BuildConnectionString(dbConnObj.DataSource, string.Empty, dbConnObj.CatalogName, dbConnObj.AuthType, dbConnObj.UserID, dbConnObj.Password);
                string tblName = tblTn.Text;
                if (this.ViewTableStatisticsEvent != null)
                {
                    this.ViewTableStatisticsEvent(connStr, tblName);
                }
            }
        }
        #endregion

        #region views
        void mnuDropView_Click(object sender, EventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        void mnuRenameView_Click(object sender, EventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        void mnuOpenView_Click(object sender, EventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        private void RefreshViews(TreeNode viewRootTN)
        {
            if (this.tree_Source.SelectedNode != null && this.tree_Source.SelectedNode.ImageIndex == 2)
            {
                DbConnection dbConnObj = (DbConnection)this.tree_Source.SelectedNode.Tag;
                string connStr = DbConnUtil.BuildConnectionString(dbConnObj.DataSource, string.Empty, dbConnObj.CatalogName, dbConnObj.AuthType, dbConnObj.UserID, dbConnObj.Password);

                string[] viewNames = DbUtil.GetViews(connStr);
                Hashtable htViews = new Hashtable();
                if (viewNames != null && viewNames.Length > 0)
                {
                    foreach (string viewName in viewNames)
                    {
                        htViews.Add(viewName.ToLower(), dbConnObj);
                    }
                }


                if (viewRootTN.Nodes.Count > 0)
                {
                    foreach (TreeNode viewTn in viewRootTN.Nodes)
                    {
                        if (htViews.Contains(viewTn.Text.ToLower()))
                        {
                            htViews.Remove(viewTn.Text.ToLower());
                        }
                    }
                }
                if (htViews.Count > 0)
                {
                    for (int i = 0; i < viewNames.Length; i++)
                    {
                        if (htViews.Contains(viewNames[i].ToLower()))
                        {
                            TreeNode viewTn = new TreeNode(viewNames[i]);
                            viewTn.ImageIndex = 4;
                            viewTn.SelectedImageIndex = 4;
                            viewTn.Tag = htViews[viewNames[i].ToLower()];
                            viewRootTN.Nodes.Add(viewTn);
                        }
                    }
                }
            }
        }

        private void RefreshView(TreeNode viewTN)
        {
            if (this.tree_Source.SelectedNode != null && this.tree_Source.SelectedNode.ImageIndex == 4)
            {
                TreeNode viewTn = this.tree_Source.SelectedNode;
                DbConnection dbConnObj = (DbConnection)viewTn.Tag;
                string connStr = DbConnUtil.BuildConnectionString(dbConnObj.DataSource, string.Empty, dbConnObj.CatalogName, dbConnObj.AuthType, dbConnObj.UserID, dbConnObj.Password);
                string viewName = viewTn.Text;
                TableColumn[] cols = DbUtil.GetTableColumns(DataProviderType.MSSQL, connStr, string.Empty, viewName);
                viewTn.Nodes.Clear();
                foreach (TableColumn col in cols)
                {
                    string nodeText = col.ColumnName + " (" + col.DbType.ToString() + ")";
                    TreeNode colTn = new TreeNode(nodeText);
                    colTn.Tag = col.ColumnName;
                    colTn.ImageIndex = 7;
                    colTn.SelectedImageIndex = 7;
                    if (col.IsPK)
                    {
                        colTn.ImageIndex = 19;
                        colTn.SelectedImageIndex = 19;
                    }
                    else
                    {
                        switch (col.DbType)
                        {
                            case System.Data.SqlDbType.BigInt:
                            case System.Data.SqlDbType.Decimal:
                            case System.Data.SqlDbType.Float:
                            case System.Data.SqlDbType.Int:
                            case System.Data.SqlDbType.Money:
                            case System.Data.SqlDbType.Real:
                            case System.Data.SqlDbType.SmallInt:
                            case System.Data.SqlDbType.SmallMoney:
                            case System.Data.SqlDbType.TinyInt:
                                colTn.ImageIndex = 8;
                                colTn.SelectedImageIndex = 8;
                                break;
                            case SqlDbType.Char:
                            case SqlDbType.NChar:
                            case SqlDbType.NText:
                            case SqlDbType.NVarChar:
                            case SqlDbType.Text:
                            case SqlDbType.VarChar:
                                colTn.ImageIndex = 7;
                                colTn.SelectedImageIndex = 7;
                                break;
                            case SqlDbType.Bit:
                                colTn.ImageIndex = 9;
                                colTn.SelectedImageIndex = 9;
                                break;
                            case SqlDbType.DateTime:
                            case SqlDbType.SmallDateTime:
                                colTn.ImageIndex = 10;
                                colTn.SelectedImageIndex = 10;
                                break;
                            default:
                                break;
                        }
                    }
                    viewTn.Nodes.Add(colTn);
                }
            }
        }
        #endregion

        #region refresh
        void mnuRefresh_Click(object sender, EventArgs e)
        {
            switch (this.tree_Source.SelectedNode.ImageIndex)
            {
                case 0:     // server
                    this.RefreshServer(this.tree_Source.SelectedNode);
                    break;
                case 1:     // database
                    this.RefreshDatabase(this.tree_Source.SelectedNode);
                    break;
                case 2:     // folder
                    if (this.tree_Source.SelectedNode.Text.ToLower() == "tables")
                    {
                        this.RefreshTables(this.tree_Source.SelectedNode);
                    }
                    else if (this.tree_Source.SelectedNode.Text.ToLower() == "views")
                    {
                        this.RefreshViews(this.tree_Source.SelectedNode);
                    }
                    break;
                case 3:
                    this.RefreshTable(this.tree_Source.SelectedNode);
                    break;
                case 4:
                    this.RefreshView(this.tree_Source.SelectedNode);
                    break;
                default:
                    break;
            }
        }

        private void tree_Source_DoubleClick(object sender, EventArgs e)
        {
            if (this.tree_Source.SelectedNode != null)
            {
                switch (this.tree_Source.SelectedNode.ImageIndex)
                {
                    case 0:     // server
                        this.RefreshServer(this.tree_Source.SelectedNode);
                        break;
                    case 1:     // database
                        this.RefreshDatabase(this.tree_Source.SelectedNode);
                        break;
                    case 2:     // folder
                        if (this.tree_Source.SelectedNode.Text.ToLower() == "tables")
                        {
                            this.RefreshTables(this.tree_Source.SelectedNode);
                        }
                        else if (this.tree_Source.SelectedNode.Text.ToLower() == "views")
                        {
                            this.RefreshViews(this.tree_Source.SelectedNode);
                        }
                        break;
                    case 3:
                        this.RefreshTable(this.tree_Source.SelectedNode);
                        break;
                    case 4:
                        this.RefreshView(this.tree_Source.SelectedNode);
                        break;
                    default:
                        break;
                }
                this.tree_Source.SelectedNode.Expand();
            }
        }
        #endregion

        #region query window
        private void OpenNewQueryWindow(string connStr)
        {
        }
        #endregion

    }
}
